goog.provide('dkpoints.auth');

goog.require('goog.dom');
goog.require('goog.net.XhrIo');

goog.require('dkpoints.menu');

// Module name. Used by the Place manager.
dkpoints.auth.name = "/auth";

// Error handling constant
/**
 * @type{string}
 * @const
*/
dkpoints.auth.AUTHCHECKPENDING = "Authentication check pending.";

// The acceptable amount of time (ms) to wait for a checkStatus to return before
// throwing a failure.
dkpoints.auth.timeoutDelay = 1000;

dkpoints.auth.checkStatus = function() {
  return function(e) {
    var me = dkpoints.auth;
    
    if(me.Xhr.isSuccess()) {
      me.authStatus = me.Xhr.getResponseJson();
    }
    else {
      me.authStatus = {};
      
      me.authStatus['loggedIn'] = false;
      me.authStatus['isAdmin'] = false;
      me.authStatus['name'] = "";
    }
    
    dkpoints.menu.toggleAdmin(me.authStatus);
    me.pendingReq = false;
  };
};

dkpoints.auth.init = function() {
  var me = dkpoints.auth;
  
  if(! me.pendingReq) {
    me.Xhr = new goog.net.XhrIo();
    me.pendingReq = true;
    goog.events.listen(me.Xhr, goog.net.EventType.COMPLETE, me.checkStatus(), false, me);
    me.Xhr.send(dkpoints.baseURL + "/apis/auth/check");
  }
};

dkpoints.auth.loadUI = function(referer, mode) {
  var me = dkpoints.auth;
  if(mode == "login") {
    if(me.authStatus['loggedIn']) { // User is trying to log in while already logged in
      // Kick them back to where they just were.
      location.hash = referer;
    }
    else {
      // Send them to the login screen
      console.log(referer);
      var ref_reg = /^(\/.+)$/;
      if(! ref_reg.test(referer)) {
        location.href = dkpoints.baseURL + "/apis/auth/login";
      }
      else {
        var refURL = ref_reg.exec(referer)[1];
        location.href = dkpoints.baseURL + "/apis/auth/login?referer=" + referer;
      }
    }
  }
  else if(mode == "logout") {
    if(me.authStatus['loggedIn']) { // User is logged in and trying to log out

      // Log out through an invisible XHR request.
      goog.events.listen(me.Xhr, goog.net.EventType.COMPLETE, me.checkStatus(), false, me);
      me.Xhr.send(dkpoints.baseURL + "/apis/auth/logout");
      
      location.hash = referer;
      setTimeout(function() { location.reload();  }, dkpoints.auth.timeoutDelay);
    }
    else { // User is logging out while not logged in.
      // Kick them back to where they just were.
      location.hash = referer;
    }
  }
};

/**
 * Confirms the authorization level of the current user
 * 0: Not logged in
 * 1: Logged in, not admin
 * 2: Admin user.
 *
 * Throws Error if an there is a pending auth check. Calling method can
 * back off and try again later.
 */
dkpoints.auth.checkAuthLevel = function() {
  var me = dkpoints.auth;

  if(me.pendingReq) {
    throw Error(me.AUTHCHECKPENDING);
  }
  else {
    if(me.authStatus['isAdmin']) {
      return 2;
    }
    else if(me.authStatus['loggedIn']) {
      return 1;
    }
    else {
      return 0;
    }
  }
};